package com.doubi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.doubi.mapper.SysPermissionMapper;
import com.doubi.mapper.SysRolePermissionMapper;
import com.doubi.model.sys.TSysPermission;
import com.doubi.model.sys.TSysRolePermission;
import com.doubi.model.sys.TSysUser;
import com.doubi.utils.DataUtil;
import com.doubi.vo.AppModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class SysPermissionService extends ServiceImpl<SysPermissionMapper, TSysPermission> {

    @Autowired
    private SysPermissionMapper sysPermissionMapper;
    @Autowired
    private SysRolePermissionMapper sysRolePermissionMapper;

    /**
     * MrDoubi
     * 2019年10月20日 20:17:33
     * 获取用户的所有权限
     * @param sysUser bean
     * @return list
     */
    public List<TSysPermission> getMyRole(TSysUser sysUser) {
        return sysPermissionMapper.getMyRole(sysUser);
    }


    /**
     * MrDoubi
     * 2019年10月20日 20:25:53
     * 获取所有的权限列表
     * @return list
     */
    public List<TSysPermission> getAll() {
        QueryWrapper<TSysPermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sortNum");
        return sysPermissionMapper.selectList(queryWrapper);
    }

    /**
     * MrDoubi
     * 2019年10月20日 20:30:42
     * 修改和添加 权限
     * @param permission 权限对象
     * @param sysUser 用户对象
     * @return model
     */
    public AppModel saveOrUpdate(TSysPermission permission, TSysUser sysUser) {
        if (DataUtil.isEmpty(permission.getOid())) {
            permission.setCreateTime(new Date());
            permission.setCreateBy(sysUser.getOid());
            //获取父级，设置祖级
            TSysPermission parent = super.getById(permission.getParentId());
            if (DataUtil.isNotEmpty(parent.getAncestors())) {
                permission.setAncestors(parent.getAncestors() + "," + parent.getOid());
            } else {
                permission.setAncestors(parent.getOid());
            }
            sysPermissionMapper.insert(permission);
        } else {
            TSysPermission bean = sysPermissionMapper.selectById(permission.getOid());
            bean.setIconClass(permission.getIconClass());
            bean.setTitle(permission.getTitle());
            bean.setPermission(permission.getPermission());
            bean.setType(permission.getType());
            bean.setRemark(permission.getRemark());
            bean.setSortNum(permission.getSortNum());
            bean.setUrl(permission.getUrl());
            bean.setUpdateTime(new Date());
            bean.setUpdateBy(sysUser.getOid());
            sysPermissionMapper.updateById(bean);
        }
        return AppModel.success("操作成功！");
    }


    /**
     * MrDoubi
     * 2019年10月20日 20:31:07
     * 删除权限
     * @param permission bean
     * @return model
     */
    public AppModel delBean(TSysPermission permission) {
        permission = sysPermissionMapper.selectById(permission.getOid());
        /*//获取所有的下级
        List<TSysPermission> sysPermissions = sysPermissionDao.getAllByParentId(permission.getOid());
        for (TSysPermission s: sysPermissions) {
            sysRolePermissionDao.delete(TSysRolePermission.class, "sysPermissionOid", s.getOid());
            sysUserPermissionDao.delete(TSysUserPermission.class, "permissionOid", s.getOid());
            sysPermissionDao.deleteById(s.getOid());
        }
        sysRolePermissionDao.delete(TSysRolePermission.class, "sysPermissionOid", permission.getOid());
        sysUserPermissionDao.delete(TSysUserPermission.class, "permissionOid", permission.getOid());*/
        sysPermissionMapper.deleteById(permission.getOid());
        return AppModel.success("删除成功！");
    }


    /**
     * 功能说明：获取角色和权限关联
     * @author  MrDoubi 2019-01-10 15:15:49
     * @param oid oid
     * @return model
     */
    public AppModel getAllByRole(String oid) {
        return AppModel.success(sysPermissionMapper.getAllByRole(oid));
    }


    /**
     * 功能说明：角色 权限关联提交
     * @author  MrDoubi 2019-01-10 15:16:13
     * @param oid 选择的全部权限
     * @param roleOid 角色oid
     */
    public AppModel editUserRole(String[] oid, String roleOid) {
        sysRolePermissionMapper.deleteByMap(new HashMap<String, Object>(){{
            put("sys_role_oid",roleOid);
        }});
        for (String s : oid) {
            TSysRolePermission sysRolePermission = new TSysRolePermission();
            sysRolePermission.setSysRoleOid(roleOid);
            sysRolePermission.setSysPermissionOid(s);
            sysRolePermissionMapper.insert(sysRolePermission);
        }
        return AppModel.success("操作成功!");
    }
}
